package io.jpress.core.flowable;

import io.jpress.commons.flowable.FlowConsts;
import org.flowable.bpmn.model.ExtensionAttribute;
import org.flowable.bpmn.model.FlowElement;
import org.flowable.bpmn.model.FlowNode;
import org.flowable.bpmn.model.UserTask;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.impl.bpmn.behavior.MultiInstanceActivityBehavior;
import org.flowable.engine.impl.bpmn.behavior.UserTaskActivityBehavior;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class JPressUserTaskActivityBehavior extends UserTaskActivityBehavior {

    public JPressUserTaskActivityBehavior(UserTask userTask) {
        super(userTask);
    }

    @Override
    public void execute(DelegateExecution execution) {

        FlowElement flowElement = execution.getCurrentFlowElement();
        if (flowElement instanceof FlowNode) {

            FlowNode flowNode = (FlowNode) flowElement;
            Object behavior = flowNode.getBehavior();

            List<String> userIds = new ArrayList<>();
            Map<String, List<ExtensionAttribute>> attributes = flowElement.getAttributes();
            List<ExtensionAttribute> bindUserList = attributes.get("bindUserList");
            if (bindUserList != null && bindUserList.size() == 1) {
                ExtensionAttribute extensionAttribute = bindUserList.get(0);
                String value = extensionAttribute.getValue();
                String[] userIdAndNames = value.split(",");
                for (String userIdAndName : userIdAndNames) {
                    userIds.add(userIdAndName.split("#")[0].trim());
                }
            }


            //多实例, 会签
            if (behavior instanceof MultiInstanceActivityBehavior) {

                //总实例数量
                Integer nrOfInstances = (Integer) execution.getVariable("nrOfInstances");
                //已经完成的示例数量
                Integer nrOfCompletedInstances = (Integer) execution.getVariable("nrOfCompletedInstances");
                //当前的循环 index
                Integer loopCounter = (Integer) execution.getVariable("loopCounter");

                if (flowElement instanceof UserTask) {
                    ((UserTask) flowElement).setAssignee(userIds.get(loopCounter));
                }


                //第一次执行的时候
                if (nrOfInstances != null && nrOfInstances > 0 && nrOfCompletedInstances != null && nrOfCompletedInstances == 0) {
                    execution.setVariable(FlowConsts.VAR_ACTION_AGREE_COUNT, 0L);
                    execution.setVariable(FlowConsts.VAR_ACTION_REFUSE_COUNT, 0L);
                    execution.setVariable(FlowConsts.VAR_ACTION_REBUT_COUNT, 0L);
                    execution.setVariable(FlowConsts.VAR_ACTION_STOP_COUNT, 0L);
                }
            }
            //单实例
            else {
                execution.removeVariable(FlowConsts.VAR_ACTION_AGREE_COUNT);
                execution.removeVariable(FlowConsts.VAR_ACTION_REFUSE_COUNT);
                execution.removeVariable(FlowConsts.VAR_ACTION_REBUT_COUNT);
                execution.removeVariable(FlowConsts.VAR_ACTION_STOP_COUNT);
            }
        }

        super.execute(execution);
    }

//    @Override
//    public void execute(DelegateExecution execution, MigrationContext migrationContext) {
//        System.out.println("----->execute2: " +execution.getCurrentFlowElement().getId());
//        super.execute(execution, migrationContext);
//    }
//
//    @Override
//    public void leave(DelegateExecution execution) {
//        System.out.println("----->leave: " + execution.getCurrentActivityId());
//        super.leave(execution);
//    }
//
//    @Override
//    public void trigger(DelegateExecution execution, String signalName, Object signalData) {
//        System.out.println("----->trigger: " + execution.getCurrentActivityId());
//        super.trigger(execution, signalName, signalData);
//    }
}
